package boot.spring.rabbitMQ.listener;

import boot.spring.po.Mail;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.util.Map;

@Component
public class DirectAckListener implements ChannelAwareMessageListener {



    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            byte[] body = message.getBody();
            ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(body));

            Mail msgMap = (Mail) ois.readObject();

            String mailId = msgMap.getMailId();
            String country = msgMap.getCountry();
            Double weight = msgMap.getWeight();
            ois.close();

            if ("directAckQueue1".equals(message.getMessageProperties().getConsumerQueue())){
                System.out.println("消费的消息来自的队列名为："+message.getMessageProperties().getConsumerQueue());
                System.out.println("消息成功消费到  mailId:"+mailId+"  country:"+country+"  weight:"+weight);
                System.out.println("执行TestDirectQueue中的消息的业务处理流程......");

            }

            if ("directAckQueue2".equals(message.getMessageProperties().getConsumerQueue())){
                System.out.println("消费的消息来自的队列名为："+message.getMessageProperties().getConsumerQueue());
                System.out.println("消息成功消费到  mailId:"+mailId+"  country:"+country+"  weight:"+weight);
                System.out.println("执行fanout.A中的消息的业务处理流程......");

            }

            channel.basicAck(deliveryTag, true);
//			channel.basicReject(deliveryTag, true);//为true会重新放回队列
        } catch (Exception e) {
            channel.basicReject(deliveryTag, false);
            e.printStackTrace();
        }
    }

}
